PROJECT = "PROJECT"
VERSION = "1.0.0"
sys = require("sys")
 
-- 配置马达输出引脚为输出模式
-- pin = {21, 18, 17, 19}
-- for i = 1, 4 do gpio.setup(pin[i], 1) end

-- led 灯引脚
ledYellow = 7
ledGreen = 4
ledRed = 1
gpio.setup(ledYellow, 1) -- 黄 指示灯
gpio.setup(ledGreen, 1) -- 绿
gpio.setup(ledRed, 1) -- 红

-- button 按钮引脚
button1Pin = 17
button2Pin = 21

-- 触摸屏
-- 地址位 1000
local on1 = string.char(0x5A, 0xA5, 0x05, 0x82, 0x10 , 0x00, 0x00, 0x01)
local off1 = string.char(0x5A, 0xA5, 0x05, 0x82, 0x10 , 0x00, 0x00, 0x00)

-- 地址位 1001
local on2 = string.char(0x5A, 0xA5, 0x05, 0x82, 0x10 , 0x01, 0x00, 0x01)
local off2 = string.char(0x5A, 0xA5, 0x05, 0x82, 0x10 , 0x01, 0x00, 0x00)

-- 地址位 1003
local on3 = string.char(0x5A, 0xA5, 0x05, 0x82, 0x10 , 0x02, 0x00, 0x01)
local off3 = string.char(0x5A, 0xA5, 0x05, 0x82, 0x10 , 0x02, 0x00, 0x00)

-- 配置uart引脚 蓝牙
uart.setup(2, 9600)
uart.on(2, "receive", function()
-- xxxxxx:1
	local data = uart.read(2)
    local value = data:toHex()
	print("2口", data , data == 1 , data == "1",value)
	if data == "1" then
		uart.write(1, on1)
		gpio.set(ledYellow, 0)
	elseif data == "2" then
		uart.write(1, off1)
		gpio.set(ledYellow, 1)
	elseif data == "3" then
		uart.write(1, on2)
		gpio.set(ledGreen, 0)
	elseif data == "4" then
		uart.write(1, off2)
		gpio.set(ledGreen, 1)
	elseif data == "5" then
		uart.write(1, on3)
		gpio.set(ledRed, 0)
	elseif data == "6" then
		uart.write(1, off3)
		gpio.set(ledRed, 1)
	end
end)

K17 = gpio.setup(button1Pin, function(state) -- 中断模式, 上升下降都触发，开启上拉
	local down = K17() == 0
	if down then
		gpio.set(ledYellow, 0)
	else 
		gpio.set(ledYellow, 1)
	end
	log.info("gpio17", "gpio17 button",K17())
end, gpio.PULLUP,gpio.BOTH)

K21 = gpio.setup(button2Pin, function(state) -- 中断模式, 上升下降都触发，开启上拉
	local down = K21() == 0
	if down then
		gpio.set(ledGreen, 0)
	else 
		gpio.set(ledGreen, 1)
	end
	log.info("gpio21", "gpio21 button",K21())
end, gpio.PULLUP,gpio.BOTH)

-- 配置uart引脚
uart.setup(1, 115200)
uart.on(1, "receive", function()
    local val = uart.read(1)
	-- 5A A5 06 83 1000 01 0000
	-- 5A A5 06 83 1000 01 0001
	local data = nil
	local ledPin = nil
	local str = val:toHex()
	if str == "5AA506831000010001" then
		gpio.set(ledYellow, 0)
		ledPin = ledYellow
		data = off1
	elseif str == "5AA506831000010000" then
		gpio.set(ledYellow, 1)
	elseif str == "5AA506831001010001" then
		gpio.set(ledGreen, 0)
		ledPin = ledGreen
		data = off2
	elseif str == "5AA506831001010000" then
		gpio.set(ledGreen, 1)
	elseif str == "5AA506831002010001" then
		gpio.set(ledRed, 0)
		ledPin = ledRed
		data = off3
	elseif str == "5AA506831002010000" then
		gpio.set(ledRed, 1)
	end
	
	if data ~= nil then
		sys.timerStart(function() 
			gpio.set(ledPin, 1)
			uart.write(1, data)
		end, 1000)
	end
    print("1口：", val, str, str == "5AA506831000010001")
end)

 
-- 逻辑
sys.taskInit(function()
	sys.wait(2000)
	print("dddd")
	-- gpio.set(4, 0)
	-- gpio.set(1, 0)
	
	while false do 
		local down = K21() == 1
		if down then
			
		end
		sys.wait(1000)
	end
	
	while false do
		local data = on
		local v = 0
		local flag = os.time() % 2 == 0
		if flag then 
			data = off
			v = 1
		end
		print("写入：", data:toHex())
		uart.write(1, data)
		-- gpio.set(7,v)
		-- gpio.set(4,v)
		sys.wait(1000)
	end
end)

sys.run()